package com.lancoo.ccas52.schedues;

import com.lancoo.ccas52.entity.CollegeDealTime;
import com.lancoo.ccas52.entity.CssTaskInfo;
import com.lancoo.ccas52.entity.CssTaskRound;
import com.lancoo.ccas52.service.CollegeDealTimeService;
import com.lancoo.ccas52.service.CssTaskInfoService;
import com.lancoo.ccas52.service.CssTaskRoundService;
import com.lancoo.ccas52.service.YunBasicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName SendMsgSchedule
 * @Description TODO
 * @Author quin
 * @Date 2023/4/24 18:50
 */
@Component
@Slf4j
@EnableAsync(proxyTargetClass = true)
public class SendMsgSchedule implements Serializable{

    @Resource
    private CollegeDealTimeService collegeDealTimeService;
    @Resource
    private CssTaskRoundService cssTaskRoundService;
    @Resource
    private CssTaskInfoService cssTaskInfoService;


    @Resource
    private YunBasicService yunBasicService;

    /**
     * 选课结束提醒
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void sendSelectCourseEndMsg() {
        cssTaskRoundService.lambdaQuery()
                .ge(CssTaskRound::getEndTime, LocalDateTime.now().withHour(0).withMinute(0).withSecond(0))
                .le(CssTaskRound::getEndTime, LocalDateTime.now().withHour(23).withMinute(59).withSecond(59)).list()
                .forEach(cssTaskRound -> {
                    CssTaskInfo cssTaskInfo = cssTaskInfoService.getById(cssTaskRound.getCssTaskId());
                    if (cssTaskInfo.getType() == 1) {
                        yunBasicService.sendSelectCourseEndMsg(cssTaskRound.getEndTime(), cssTaskInfo.getTaskId(), "预选",null);
                    } else if (cssTaskInfo.getType() == 2) {
                        if (cssTaskInfo.getRound() == 1) {
                            yunBasicService.sendSelectCourseEndMsg(cssTaskRound.getEndTime(), cssTaskInfo.getTaskId(), "正选",cssTaskRound.getId());
                        } else if (cssTaskInfo.getRound() == 2) {
                            yunBasicService.sendSelectCourseEndMsg(cssTaskRound.getEndTime(), cssTaskInfo.getTaskId(), "补选",cssTaskRound.getId());
                        } else if (cssTaskInfo.getRound() == 3) {
                            yunBasicService.sendSelectCourseEndMsg(cssTaskRound.getEndTime(), cssTaskInfo.getTaskId(), "退选",cssTaskRound.getId());
                        }
                    }
                });
    }

    /**
     * 审核开课计划变更
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void sendCoursePlanExamineMsg() {
        List<CollegeDealTime> list = collegeDealTimeService.lambdaQuery().eq(CollegeDealTime::getTaskId, 1)
                .ge(CollegeDealTime::getSetEndTime, LocalDateTime.now().withHour(0).withMinute(0).withSecond(0))
                .le(CollegeDealTime::getSetEndTime, LocalDateTime.now().withHour(23).withMinute(59).withSecond(59)).list();
        for (CollegeDealTime collegeDealTime : list) {
            yunBasicService.sendCoursePlanExamineMsg(collegeDealTime.getTaskId());
        }
    }


}
